package activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import net.ck.hzq.R;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import Bean.AllyBean;
import Bean.AllyParamBean;
import Utils.HelloWordModel;
import Utils.SharedPrefStore;
import Utils.SystemDatas;
import adapter.FlowPartnerAdapter;
import base.BaseActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import dialog.ConfirmPartnerDialog;
import event.LoginEvent;
import event.PushBackEvent;
import retrofit2.Call;
import retrofit2.Response;

public class SelectFlowPartnerActivity extends BaseActivity {

    FlowPartnerAdapter partnerAdapter;

    @BindView(R.id.top_ss)
    EditText top_ss;//搜索内容输入框

    @BindView(R.id.usercount)
    TextView usercount;//搜索内容输入框
    List<AllyBean.DataBean.ItemsBean> questionInfo = new ArrayList<>();
    List<AllyBean.DataBean.ItemsBean> questionInfoByEdit = new ArrayList<>();
    public static void start(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, SelectFlowPartnerActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_select_flow_partner;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        findViewById(R.id.iv_back).setOnClickListener(v -> finish());

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        partnerAdapter = new FlowPartnerAdapter(this, this::showConfirmDialog);
        recyclerView.setAdapter(partnerAdapter);

        fetchCourses();

        top_ss.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                questionInfoByEdit.clear();
                for (int i = 0;i<questionInfo.size();i++){
                    if(s.toString().equals("")||questionInfo.get(i).getRealName().contains(s.toString())){
                        questionInfoByEdit.add(questionInfo.get(i));
                    }
                }
                partnerAdapter.setupData(questionInfoByEdit);
            }
        });
    }

    private void fetchCourses() {
        showProgressDialog();
        AllyParamBean a = new AllyParamBean();
        SharedPrefStore store = new SharedPrefStore();
        a.setCustomId(Integer.parseInt(store.getString("id", "0")));
        a.setKey("");
        a.setType(1);
        Call<AllyBean> userMsgCall = HelloWordModel.getInstance(this).getAlly(SystemDatas.GetService_URL("getally"), a);//, sp.getString("token", null)
        userMsgCall.enqueue(new retrofit2.Callback<AllyBean>() {
            @Override
            public void onResponse(Call<AllyBean> call, final Response<AllyBean> response) {
                dismissProgressDialog();
                try {
                    if (response.body() != null && "401".equals(response.body().getResult())) {
                        EventBus.getDefault().post(new LoginEvent());
                    } else if (response.body().getMsg().equals("success") && response.body().getData() != null) {
                        questionInfo.addAll(response.body().getData().getItems());
                        partnerAdapter.setupData(questionInfo);
                        usercount.setText("合伙人  共"+response.body().getData().getItems().size()+"人");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<AllyBean> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

    private void showConfirmDialog(String id, String partnerName) {
        ConfirmPartnerDialog dialog = ConfirmPartnerDialog.newInstance(partnerName);


        dialog.setConfirmListener(() -> {
            EventBus.getDefault().post(new PushBackEvent(id));
            Intent data = new Intent();
            data.putExtra("partnerId", id);
            setResult(Activity.RESULT_OK, data);
            finish();
        });
        dialog.show(getSupportFragmentManager(), "");
    }

}
